package com.saianfu.flowable.demo.controller

import com.saianfu.flowable.demo.common.impl.InstanceHandler
import com.saianfu.flowable.demo.common.impl.TaskHandler
import com.saianfu.flowable.demo.common.impl.TaskQueryHandler
import com.saianfu.flowable.demo.controller.result.ResponseData
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.flowable.engine.task.Comment
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 *
 *
 * 流程任务相关
 *
 */
@RestController
@RequestMapping("api/flow/task")
@Api(value = "Task", tags = ["流程任务"])
class TaskController {
    @Autowired
    lateinit var taskHandler: TaskHandler

    @Autowired
    lateinit var taskQueryHandler: TaskQueryHandler

    @RequestMapping(value = ["/execute"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "执行任务", notes = """部门领导审批：{"days":"6","check":"1","actorIds":["wangwu","lifei"]}
分管领导审批：{"check":"1","actorIds":["hr_li","hr_zhang"]}""", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun executeTask(taskId: String, @RequestBody variables: Map<String, Any?>): ResponseData<*> {
        val map = taskHandler.complete(taskId, variables, false)
        return ResponseData.success(map)
    }

    @RequestMapping(value = ["/claim"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "任务签收", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"), ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun claim(taskId: String, userId: String): ResponseData<*> {
        taskHandler.claim(taskId, userId)
        return ResponseData.success("任务签收成功")
    }

    @RequestMapping(value = ["/unclaim"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "任务反签收", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun unclaim(taskId: String): ResponseData<*> {
        taskHandler.unclaim(taskId)
        return ResponseData.success("任务反签收成功")
    }

    @RequestMapping(value = ["/delegate"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "任务委派", notes = "【act_ru_task】委派人：owner，被委派人：assignee，委派任务：delegateTask，任务回到委派人：resolveTask", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"), ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun delegate(taskId: String, userId: String): ResponseData<*> {
        taskHandler.delegate(taskId, userId)
        return ResponseData.success("任务委派成功")
    }

    @RequestMapping(value = ["/resolve"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "任务归还：被委派人完成任务之后，将任务归还委派人", notes = "", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun reslove(taskId: String): ResponseData<*> {
        taskHandler.resolveTask(taskId)
        return ResponseData.success()
    }

    @RequestMapping(value = ["/assignee "], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "任务转办", notes = "【act_ru_task】设置assignee为转办人", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"), ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun assignee(taskId: String, userId: String): ResponseData<*> {
        taskHandler.setAssignee(taskId, userId)
        return ResponseData.success("任务转办成功")
    }

    @RequestMapping(value = ["/query/taskId "], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "任务查询", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun assignee(taskId: String): ResponseData<*> {
        val task = taskQueryHandler.taskId(taskId)
        //TaskVO taskVO = new TaskVO();
        //BeanUtils.copyProperties(task,taskVO);
        return ResponseData.success(task)
    }

    @RequestMapping(value = ["/comment/query "], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "查询批注信息", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun getTaskComments(taskId: String): ResponseData<*> {
        val taskComments = taskHandler.getTaskComments(taskId)
        return ResponseData.success(taskComments)
    }

    @RequestMapping(value = ["/comment/add"], method = [RequestMethod.POST])
    @ResponseBody
    @ApiOperation(value = "添加批注信息", notes = "批注信息：act_hi_comment", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "taskId", value = "任务ID", required = true, dataType = "String"), ApiImplicitParam(name = "instanceId", value = "实例ID", required = true, dataType = "String"), ApiImplicitParam(name = "message", value = "批注内容", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun addComment(taskId: String, instanceId: String, message: String?): ResponseData<Comment?> {
        val comment = taskHandler.addComment(taskId, instanceId, message)
        return ResponseData.success(comment)
    }

    companion object {
        protected var logger = LoggerFactory.getLogger(InstanceHandler::class.java)
    }
}